<?php 

class Router {


	public static function url($class_name, $params = array()) {
		$path = defined("IN_CMS") && IN_CMS ? CMS_URL . "index.php" : BASE_URL . "/pages/index.php";
	
		if( is_array($params) ) {
			
			if( isset($params['module']) ) {
				$path .= "?module=" . $params['module'];
				unset($params['module']);
			}
			
			else if( isset($params['controller']) ){
				$path .= "?controller=" . $params['controller'];
				unset($params['controller']);
			}
			
			else {
				$class_name = str_replace(array('module','controller'), "", strtolower($class_name));
				$path .= "?module=" . $class_name;
			}
			
			if( isset($params['action']) ) {
				$path .= "&action=" . $params['action'];
				unset($params['action']);
			}
			
			// append any extra parameters
			foreach($params as $param => $value) {
				$path .= "&" . $param . "=" . urlencode($value);
			}
		}
		
		else {
			$class_name = str_replace(array('module','controller'), "", strtolower($class_name));
			$path .= "?module="	. $class_name;
		}
		
		return $path;
	}	
	
	public static function redirect($params = array()) {
		$path = Router::url($params);
		
		header("Location: $path");
	}
}
?>